import 'package:fluro/fluro.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:just_audio/just_audio.dart';
import 'package:wymusic/Util/localStorage.dart';
import 'package:wymusic/pages/home.dart';
import 'package:wymusic/pages/personCenter.dart';
import 'package:wymusic/router/routeStatic.dart';
import 'package:wymusic/router/router_handler.dart';
import 'package:wymusic/router/routers.dart';

class App extends StatefulWidget {
  App({Key? key, required this.player}) : super(key: key);
  AudioPlayer player;
  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> with TickerProviderStateMixin {
  List<Widget> pageList = []; // 页面
  int pageIndex = 0; // 页面索引
  List<String> title = ['音乐', '个人中心']; // 标题
  String id = ''; // 全局id
  List<String> musicList = []; // 全局音乐ids
  bool isFavorite = false; // 是否喜欢
  bool playState = true; // 是否播放
  @override
  void initState() {
    super.initState();
    // 初始化页面
    print('初始化页面');
    pageList.add(Home(player: widget.player));
    pageList.add(Person());
    widget.player.setAudioSource(
      ConcatenatingAudioSource(
        // Start loading next item just before reaching it.
        useLazyPreparation: true, // default
        // Customise the shuffle algorithm.
        shuffleOrder: DefaultShuffleOrder(), // default
        // Specify the items in the playlist.
        children: [
          AudioSource.uri(Uri.parse(
              "https://vkceyugu.cdn.bspapp.com/VKCEYUGU-479328cb-417a-467c-9512-83793cb72c1e/313b3fdf-cd6e-4d4b-a486-84a13c61130c.flac")),
        ],
      ),
      // Playback will be prepared to start from track1.mp3
      initialIndex: 0, // default
      // Playback will be prepared to start from position zero.
      initialPosition: Duration.zero, // default
    );
    widget.player.play();
    widget.player.setLoopMode(LoopMode.all);
    // widget.player.playerStateStream.listen((event) {});
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    FluroRouter router = FluroRouter();
    Routers.configRoute(router);
    StaticRoute.router = router;
    return Scaffold(
      appBar: AppBar(
        title: Text(title[pageIndex]),
        // leading: Icon(Icons.accessibility),
        backgroundColor: const Color.fromARGB(255, 192, 0, 0),
        shadowColor: const Color.fromARGB(0, 41, 41, 41),
        // brightness: Brightness.light,W
      ),
      body: IndexedStack(children: pageList, index: pageIndex),
      bottomNavigationBar: BottomNavigationBar(
        selectedItemColor: const Color.fromARGB(255, 192, 0, 0),
        items: [
          BottomNavigationBarItem(
              icon: const Icon(Icons.music_note_rounded), label: title[0]),
          BottomNavigationBarItem(
              icon: const Icon(Icons.person), label: title[1]),
        ],
        onTap: (value) {
          if (value == 1) {
            print(value);
            print(LocalStorage.getItem('token'));
            LocalStorage.getItem('token').then((res) {
              print(res);
              if (res == null) {
                StaticRoute.router.navigateTo(context, '/login',
                    transition: TransitionType.materialFullScreenDialog);
              }
            });
          }
          setState(() {
            pageIndex = value;
          });
        },
        currentIndex: pageIndex,
      ),
      floatingActionButton: SizedBox(
        height: 70,
        width: 70,
        child: FloatingActionButton(
          onPressed: () {
            // if(player.playerState)
          },
          backgroundColor: Color.fromARGB(255, 192, 0, 0),
          child: InkWell(
            child: IconButton(
              color: Color.fromARGB(255, 192, 0, 0),
              onPressed: () {},
              icon: Icon(
                playState ? Icons.pause : Icons.play_arrow_rounded,
                color: Color.fromARGB(255, 255, 255, 255),
                size: 30,
              ),
            ),
            onTap: () {
              print(widget.player.playerState);
              if (playState) {
                widget.player.pause();
              } else {
                widget.player.play();
              }
              setState(() {
                playState = widget.player.playing;
              });
            },
            onLongPress: () {},
          ),
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      floatingActionButtonAnimator: FloatingActionButtonAnimator.scaling,
      drawer: Drawer(
          child: Padding(
        padding: EdgeInsets.fromLTRB(0, 100, 0, 0),
        child: Column(
          children: [
            Card(
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadiusDirectional.circular(20),
              ),
              clipBehavior: Clip.antiAlias,
              child: Image.network(
                'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-479328cb-417a-467c-9512-83793cb72c1e/0873fa01-cc42-4d76-b37e-a9ae75152a20.jpg',
                width: 100,
                height: 100,
                fit: BoxFit.cover,
              ),
            ),
            Center(
              child: CupertinoButton(
                child: Text('往生堂第二碑半价'),
                onPressed: () {},
              ),
            ),
          ],
        ),
      )),
    );
  }
}
